﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.IO;
using System.Text.RegularExpressions;
namespace Tickz.IOz
{
    public class Pathz
    {
        /// <summary>
        /// 获得当前绝对路径
        /// </summary>
        /// <param name="strPath">指定的路径</param>
        /// <returns>绝对路径</returns>
        public static string GetMapPath(string strPath)
        {
            if (HttpContext.Current != null)
            {
                return HttpContext.Current.Server.MapPath(strPath);
            }
            else //非web程序引用
            {
                strPath = strPath.Replace("/", "\\");
                if (strPath.StartsWith("\\"))
                {
                    strPath = strPath.Substring(strPath.IndexOf('\\', 1)).TrimStart('\\');
                }
                return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
            }
        }
        /// <summary>
        /// 获得的路径必须有最后面的结束符号 \\
        /// eg:c:/uu 返回 c:/uu\\ 
        /// </summary>
        /// <param name="path">路径</param>
        /// <returns>有最后面的结束符号的路径</returns>
        public static string GetPathHasBehind(string path)
        {
            path= Regex.Replace(path, "([^a-z0-9]+)$", "", RegexOptions.Singleline | RegexOptions.IgnoreCase);
            return path + Path.DirectorySeparatorChar.ToString();
            
        }

        /// <summary>
        /// 获得的路径必须有最后面的结束符号 \\
        /// eg:c:/uu 返回 c:/uu\\ 
        /// </summary>
        /// <param name="path">路径</param>
        /// <returns>有最后面的结束符号的路径</returns>
        public static string GetPathHasBefore(string path)
        {
            path= Regex.Replace(path, "^[^a-z0-9]+", "", RegexOptions.Singleline | RegexOptions.IgnoreCase);
            return Path.DirectorySeparatorChar.ToString() + path;

        }
    }
}
